﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C4F.DevKit.Calendar.Parser
{
    internal class DateParser
    {
        #region Private Static Members

        private static string utcFormat = "yyyyMMdd\\THHmmss\\Z";

        private static string localDateFormat = "yyyyMMdd";

        private static string localDateTimeFormat = "yyyyMMdd\\THHmmss";

        private static string[] localFormats = {localDateFormat, localDateTimeFormat};

        #endregion

        #region Public Static Methods

        public static DateTime ParseUTC(string line)
        {
            return DateTime.SpecifyKind(DateTime.ParseExact(line, utcFormat, null), DateTimeKind.Utc);
        }

        public static DateTime ParseLocalTime(string line, int offset)
        {
            DateTime localDate = DateTime.ParseExact(line, localFormats, null, System.Globalization.DateTimeStyles.None);

            if (localDate.IsDaylightSavingTime())
            {
                offset = offset + 1;
            }

            return DateTime.SpecifyKind(localDate.AddHours(-1 * offset), DateTimeKind.Utc);
        }

        public static DateTime ParseDate(string line)
        {
            return DateTime.ParseExact(line, localDateFormat, null);
        }

        public static string ToUTCString(DateTime time)
        {
            return time.ToUniversalTime().ToString(utcFormat);
        }

        public static string ToDateString(DateTime time)
        {
            return time.ToString(localDateFormat);
        }

        #endregion
    }
}
